package com.bakend.common.config.security.login;

import com.bakend.modules.sys.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

public class CustomerUserDetails extends User implements UserDetails {


    private boolean enable;
    private boolean forbidon;
    private  Set<GrantedAuthority> authorities = new HashSet<>();

    public CustomerUserDetails(User user){
        BeanUtils.copyProperties(user,this);
        int i = Integer.parseInt(user.getStatus());
        this.enable = (i & 1)==1;
        this.forbidon = (i>>1 & 1)==1;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(Set<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public String getPassword() {
        return this.getPwd();
    }

    @Override
    public String getUsername() {
        return getUserCode();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enable;
    }

    public boolean isForbidon(){
        return forbidon;
    }
    public Map<String,Object> toClaimMap(){
        Map<String,Object> claimMap = new HashMap<>();

        claimMap.put("userName",this.getUsername());
        claimMap.put("userCode",this.getUserCode());
        claimMap.put("lastLoginIp",this.getLastLoginIp());
        claimMap.put("ipArean",this.getIpArean());
        return claimMap;
    }
}
